package com.rongmai.web.ctrl;


import com.rongmai.web.bo.TLSSigAPIv2;
import com.rongmai.db.model.BaseResult;
import com.rongmai.web.bo.MessageBo;
import com.rongmai.web.bo.UserBo;
import com.rongmai.web.dto.MessageDTO;
import com.rongmai.db.exception.BzException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageCtrl {

    @Resource
    private MessageBo messageBo;
    @Resource
    private UserBo userBo;
    @Resource
    private TLSSigAPIv2 tlsSigAPIv2;

    /**
     * 一键登录
     * @param token
     * @param request
     * @return
     */
    @RequestMapping("getMobile")
    public BaseResult<MessageDTO> getMobile(String token, HttpServletRequest request) {
        try {
            String mobile = messageBo.getMobile(token);
            return BaseResult.succ(userBo.login(mobile, request));
        } catch (BzException e) {
            log.error("业务异常", e);
            return BaseResult.fail(e.getMessage());
        } catch (Exception e) {
            log.error("系统异常", e);
            return BaseResult.FAIL;
        }
    }

    @RequestMapping("sendSms")
    public BaseResult sendSms(String mobile, HttpServletRequest request) {
        try {
            messageBo.sentSms(mobile);
        } catch (BzException e) {
            return BaseResult.fail(e.getMessage());
        } catch (Exception e) {
            return BaseResult.FAIL;
        }
        return BaseResult.succ();
    }

    @RequestMapping("checkSms")
    public BaseResult checkSms(String mobile, String smsCode, HttpServletRequest request) {
        try {
            messageBo.checkSms(mobile, smsCode);
        } catch (BzException e) {
            return BaseResult.fail(e.getMessage());
        } catch (Exception e) {
            return BaseResult.FAIL;
        }
        return BaseResult.succ();
    }


    @RequestMapping("genUserSig")
    public BaseResult genUserSig(String userId) {
        try {
            String sign = tlsSigAPIv2.genUserSig(userId);
            return BaseResult.succ(sign);
        } catch (BzException e) {
            return BaseResult.fail(e.getMessage());
        } catch (Exception e) {
            return BaseResult.FAIL;
        }
    }


}
